﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using AplicacionMVC.Models;

namespace AplicacionMVC.Controllers
{
    public class SalaController : Controller
    {


        private string salaRESTService = "http://localhost:1178/Salas.svc/Salas";
        JavaScriptSerializer js = new JavaScriptSerializer();
        // GET: /Sede/
        public ActionResult Index()
        {
            //RecursosWS.RecursoClient proxy = new RecursosWS.RecursoClient();


            HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(salaRESTService);
            req2.Method = "GET";
            HttpWebResponse res2 = (HttpWebResponse)req2.GetResponse();
            StreamReader reader2 = new StreamReader(res2.GetResponseStream());
            string salaJson = reader2.ReadToEnd();
            List<Sala> salaJson2 = js.Deserialize<List<Sala>>(salaJson);


            return View(salaJson2);


        }


        // GET: /Sede/Details/5
        public ActionResult Details(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRESTService + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string salaJson = reader.ReadToEnd();
            Sala sala = js.Deserialize<Sala>(salaJson);
            return View(sala);
        }

        // GET: /Sede/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Sede/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            //String validar = collection["nombre"];
            string salaRESTService1 = "http://localhost:1178/Salas.svc/Sala";
            HttpWebRequest req3 = (HttpWebRequest)WebRequest.Create(salaRESTService1 + "/" + collection["nombre"]);
            req3.Method = "GET";
            HttpWebResponse res3 = (HttpWebResponse)req3.GetResponse();
            StreamReader reader3 = new StreamReader(res3.GetResponseStream());
            string salaJson3 = reader3.ReadToEnd();
            Sala sala1 = js.Deserialize<Sala>(salaJson3);
            Sala sala3 = new Sala();




            if (sala1 != null)
            {
                sala3.Id_sala = int.Parse(collection["id_sala"]);
                sala3.Nombre = collection["nombre"];
                sala3.Tiposala = collection["tiposala"];
                sala3.Horario = collection["horario"];
                Session["data"] = "Sala ya creada " + sala1.Nombre;

                return View(sala3);
            }


            try
            {
                string postdata = "{\"id_sala\":\"" + collection["id_sala"] + "\",\"nombre\":\"" + collection["nombre"] + "\",\"tiposala\":\"" + collection["tiposala"] + "\",\"horario\":\"" + collection["horario"] + "\",\"estado\":\"1\"}";
                // string postdata1 = "{\"Codigo\":\"" + collection["Codigo"] + "\",\"Nombre\":\"" + collection["Nombre"] + "\",\"Tiposala\":\"" + collection["Tiposala"] + "\",\"Horario\":\"" + collection["Horario"] + "\",\"Estado\":\"" + collection["Nombre"] + "\"}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRESTService);
                req.Method = "POST";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();

                HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(salaRESTService + "/" + collection["id_sala"]);
                req.Method = "GET";
                HttpWebResponse res2 = (HttpWebResponse)req.GetResponse();
                StreamReader reader2 = new StreamReader(res.GetResponseStream());
                string salaJson2 = reader2.ReadToEnd();
                Sala sala = js.Deserialize<Sala>(salaJson2);

                Session["data"] = "Sala creada " + sala.Nombre;
                return RedirectToAction("Index");

            }
            catch (Exception e)
            {
                ModelState.AddModelError("Error", e.Message);
                return View();


            }


        }

        // GET: /Sala/Edit/5 
        public ActionResult Edit(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRESTService + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string salaJson = reader.ReadToEnd();
            Sala sala = js.Deserialize<Sala>(salaJson);
            Session["data"] = "";
            return View(sala);
        }

        // POST: //Sala/Edit/5
        [HttpPost]
        public ActionResult Edit(string id, FormCollection collection)
        {
            try
            {
                //string postdata = "{\"Id_Sala\":\"" + collection["Id_sala"] + "\",\"Nombre\":\"" + collection["Nombre"] + "\",\"Tiposala\":\"" + collection["Tiposala"] + "\",\"Horario\":\"" + collection["Horario"] + "\",\"Estado\":\"1\"}";
                string postdata = "{\"id_sala\":\"" + collection["id_sala"] + "\",\"nombre\":\"" + collection["nombre"] + "\",\"tiposala\":\"" + collection["tiposala"] + "\",\"horario\":\"" + collection["horario"] + "\",\"estado\":\"" + collection["estado"] + "\"}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRESTService);
                req.Method = "PUT";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: /Sala/Delete/5

        public ActionResult Delete(string id)
        {

            try
            {
                string postdata = "\"" + id + "\"";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRESTService);
                req.Method = "DELETE";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                Session["data"] = "";
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Error", e.Message);
                return View();
            }


        }

        // POST: /Sala/Delete/5
        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                string postdata = "\"" + id + "\"";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRESTService);
                req.Method = "DELETE";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Error", e.Message);
                return View();
            }
        }


    }
}
